package com.study.lintcode.my.code_10;

/**********************************************************************
 * &lt;p&gt;文件名：Example_1.java &lt;/p&gt;
 * &lt;p&gt;文件描述：(https://www.lintcode.com/problem/a-b-problem/)
 * @project_name：LintCode
 * @author zengshunyao
 * @create 2019/1/5 21:56
 * @history
 * @department：政务事业部
 * Copyright ChengDu Funi Cloud Code Technology Development CO.,LTD 2014
 *                    All Rights Reserved.
 */
public class Example_3 {
    /**
     * @param k: An integer 0-9
     * @param n: An integer >=1
     * @return: An integer denote the count of digit k in 0..n
     */
    public int digitCounts(int k, int n) {
        // write your code here
        int count = 0;
        if (k == 0) {
            count++;
            while (n / 10 > 0) {
                count += n / 10;
                n = n / 10;
            }
            return count;
        }
        for (int i = 0, j = 0; i <= n; i++) {
            j = i;
//            123-> 3,2,1
            while (j > 0) {
                if ((j % 10 == k)) {
                    count++;
                }
                j /= 10;
            }
        }
        return count;
    }


    public static void main(String[] args) {
        //0,0=>1
        //0,9=>1
        //1,12=>5
        //2,12=2
        System.out.println(new Example_3().digitCounts(2, 12));
    }
}
